//定义关于counter的store
import { defineStore } from "pinia";
import { Service } from "../requests/index.ts";
import { message } from "ant-design-vue";
import project from "./project.js";

/*defineStore 是需要传参数的，其中第一个参数是id，就是一个唯一的值，
简单点说就可以理解成是一个命名空间.
第二个参数就是一个对象，里面有三个模块需要处理，第一个是 state，
第二个是 getters，第三个是 actions。
*/
const useWDM = defineStore("wdm", {
  state: () => ({
    wdmList: [], //WDM列表
    currentWDM: {}, //当前WDM名称
    currentNet: "NETONE", //当前网络
  }),

  getters: {},

  actions: {
    //基本使用
    selectCurrentWDM(name) {
      return (this.currentWDM.name = name);
    },
    //获取WDM列表
    async getWDMList(projectName, net = "NETONE") {
      this.currentNet = net;
      const res = await Service.queryLinkUsingGet(projectName, "WDM", net);
      if (res.respCode === 200) {
        console.log("getWDMList", res);
        this.wdmList = res.data;
      } else {
        message.error(res.message);
      }
      return res.data;
    },
    //添加单个WDM
    async addSingleWDM(projectName, wdm, net = null) {
      const currentNetValue = net || this.currentNet;
      // console.log("wdm",wdm)
      const res = await Service.uploadSingWdmLinkUsingPost(
        projectName,
        wdm,
        currentNetValue
      );
      console.log("addSingleWDM", res);
      if (res.respCode === 200) {
        this.wdmList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("addSingleWDM",this.wdmList)
      return this.wdmList;
    },

    //文件上传
    async uploadWDMFile(file, projectName, net = null) {
      const currentNetValue = net || this.currentNet;
      // console.log("WDM",fiber)
      const res = await Service.uploadLinkUsingPost(
        file,
        projectName,
        "WDM",
        currentNetValue
      );
      if (res.respCode === 200) {
        message.success(file.name + "上传成功");
        this.wdmList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("uploadWDMFile",res)
      return this.wdmList;
    },
    //删除WDM
    async delSingleWDM(projectName, name, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.deleteNetUsingDelete(
        projectName,
        name,
        "WDM",
        currentNetValue
      );
      console.log("deleteWDMUsingDelete", res);
      if (res.respCode === 200) {
        this.wdmList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.wdmList;
    },

    //修改单个WDM
    async updateSingleWDM(projectName, wdm, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.modifyFiberUsingPut1(
        projectName,
        wdm,
        currentNetValue
      );
      console.log("updateSingleWDM", res);
      if (res.respCode === 200) {
        this.wdmList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.wdmList;
    },
  },
});

//暴露这个wdm模块
export default useWDM;
